import {Component} from "@angular/core";
import {LocalDataSource} from "ng2-smart-table";
import {SmartTableData} from "../../@core/data/smart-table";
import {ActivatedRoute} from "@angular/router";
import {NbDateService} from "@nebular/theme";
import {AdminService} from "../../AdminService";
import {BillService} from "../../energy/api";
import {BaseRequest} from "../../utils/BaseRequest";
import {map} from "rxjs/operators";
import {ResponseUnitChargeBill} from "../../energy/api/model/responseUnitChargeBill";
import {ResponseBillResult} from "../../energy/api/model/responseBillResult";
import Data = google.maps.Data;
import {start} from "repl";

@Component({
  templateUrl: './bill-result.component.html',
  styles:[
    `
      nb-card {
        transform: translate3d(0, 0, 0);
      }
      .margin {
        margin:10px;
      }
    `
  ]
})
export class BillResultComponent {
  settings = {
    pager:{
      display:true,
      perPage: 20,
    },
    hideSubHeader: true,
    actions: {
      add:false,
      edit: false,
      delete:false,
      columnTitle: '操作',
      position: 'right',
    },
    columns: {
      billNo: {
        title: '账单编号',
        type: 'string',
      },
      billAmt: {
        title: '缴费金额',
        type: 'string',
      },
      billDate: {
        title: '缴费日期',
        type: 'string',
      },
      remark:{
      	title: '标注',
	type: 'string',
      },
      extInfo: {
        title: '额外信息',
        type: 'string',
      },
    },
  };
  source: DataDataSource;// = new DataDataSource();
  unitId?:number=null;
  startTime:Date;
  endTime:Date;
  billNo:string = '';
  constructor(private service: SmartTableData
    ,private api:BillService
    ,private route:ActivatedRoute
    ,private dateService: NbDateService<Date>
    ,private admin:AdminService) {
    this.startTime = this.dateService.addDay(this.dateService.today(), -5);
    this.endTime = this.dateService.today();
    this.source = new DataDataSource(this.api,this.dateService,this.startTime,this.endTime,this.billNo,this.unitId);
  }

  filter(){
    if(this.startTime!=null)
      this.source.startTime = this.startTime;
    if(this.endTime!=null)
      this.source.endTime = this.endTime;
    if(this.billNo!=null)
      this.source.billNo = this.billNo;
    if(this.unitId!=null)
      this.source.unitId = this.unitId;
    this.source.setPage(1);
    this.source.refresh();
  }
}

export class DataDataSource extends LocalDataSource {
  all: number = -1;
  constructor(private api: BillService,private dateService: NbDateService<Date>,public startTime:Date,public endTime:Date,public billNo:string,public unitId?:number) {
    super()
  }

  count(): number {
    return this.all;
  }

  getElements(): Promise<any> {
    const query = {
      pageIndex: (this.pagingConf['page'] - 1) * this.pagingConf['perPage'],
      pageSize: this.pagingConf['perPage'],
      startTime:this.dateService.format(this.startTime,"yyyyMMdd"),
      endTime:this.dateService.format(this.endTime,'yyyyMMdd')
    };
    if(this.unitId!=null) query['unitId']=this.unitId;
    if(this.billNo!=null) query['billNo'] = this.billNo;
    return this.api.requestECABillResult(new BaseRequest(query)).pipe(map((res:ResponseBillResult)=>{
      this.all = res.dataCount;
      return res.data;
    })).toPromise();
  }
}
